# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""Convert NQ data."""
import gzip
import json
import multiprocessing
import os
import random
from tqdm import tqdm

from absl import app
from absl import flags
from glob import glob

import bs4
# import tensorflow.compat.v1 as tf
from preprocessing.common_utils import save_to_jsonl

FLAGS = flags.FLAGS

flags.DEFINE_string(
    "input_pattern",
    "Natural_Questions/dev_head_100.jsonl.gz",
    "Input path.",
)
# enwiki-20181220/orqa_out
flags.DEFINE_string("output_path", "orqa_out/NaturalQuestions-dev.txt", "Output path")

flags.DEFINE_integer("max_threads", 1, "Maximum workers in the pool.")
flags.DEFINE_integer("max_tokens", 5, "Maximum tokens in each short answer.")
flags.DEFINE_boolean("fork_workers", True, "Fork workers for more parallelism.")

# 本文件只构建 pair只包含不长于max_tokens的答案span所在的html原文


def _convert_qa_pairs(input_path):
    """Generate TF examples."""
    # tf.logging.info("Converting examples in %s.", input_path)
    pairs = []
    with gzip.open(input_path) as input_file:
        for line in tqdm(input_file, desc="loading qa pairs"):
            json_example = json.loads(line)
            question_text = json_example["question_text"]

            # Convert to bytes so that we can index by byte offsets from the data.
            document_html = json_example["document_html"].encode("utf-8")

            answer_texts = set()
            for annotation in json_example["annotations"]:
                for sa in annotation["short_answers"]:
                    if (
                        sa["end_token"] - sa["start_token"] <= FLAGS.max_tokens
                    ):  # 如果答案span满足条件，截取html中答案对应位置的text
                        raw_html = document_html[sa["start_byte"] : sa["end_byte"]]
                        answer_texts.add(bs4.BeautifulSoup(raw_html, "lxml").text)
            if answer_texts:  # 返回的pair只包含不长于max_tokens的答案span所在的html原文
                pairs.append(dict(question=question_text, answer=list(answer_texts)))
    # tf.logging.info("Done converting examples from %s", input_path)
    return pairs


def main(_):
    # input_paths = tf.gfile.Glob(FLAGS.input_pattern)
    input_paths = glob(FLAGS.input_pattern)
    # tf.logging.info("Converting input %d files: %s", len(input_paths), str(input_paths))
    num_threads = min(FLAGS.max_threads, len(input_paths))
    if num_threads > 1:
        if FLAGS.fork_workers:
            pool = multiprocessing.Pool(num_threads)
        else:
            pool = multiprocessing.dummy.Pool(num_threads)
        sharded_pairs = pool.map(_convert_qa_pairs, input_paths)

        # pylint: disable=g-complex-comprehension
        sharded_pairs = [p for l in sharded_pairs for p in l]
        # pylint: enable=g-complex-comprehension
    else:
        sharded_pairs = _convert_qa_pairs(input_paths[0])

    random.shuffle(sharded_pairs)
    # tf.logging.info("Found %d pairs.", len(sharded_pairs))
    # tf.io.gfile.makedirs(os.path.dirname(FLAGS.output_path))
    # with tf.io.gfile.GFile(FLAGS.output_path, "w") as output_file:
    #     for p in sharded_pairs:
    #         output_file.write(json.dumps(p))
    #         output_file.write("\n")
    save_to_jsonl(sharded_pairs, FLAGS.output_path)


if __name__ == "__main__":
    app.run(main)
